<?php 
## include required files
/*******************************/
require_once '../model/users.php';
require_once '../model/moduleuser.php';
require_once '../model/trade.php';
include_once("../model/budget.php");
## Paginaton class
require_once ("../includes/classes/pagination_class.php");
/*******************************/
## Create Objects
/*******************************/
$userObj = new Model_Users();
$moduleObj=new Model_ModuleUser();
$tradeObj = new Model_TRADE();
$budgetObj = new Model_Budget();
/*******************************/


function msort()
{
  $params = func_get_args();
  $array = array_pop($params);

  if (!is_array($array))
    return false;

  $multisort_params = array();
  foreach ($params as $i => $param) 
  {
    if (is_string($param)) 
    {
      ${"param_$i"} = array();
      foreach ($array as $index => $row) 
      {
        ${"param_$i"}[$index] = $row[$param];
      }
    }
    else 
      ${"param_$i"} = $params[$i];

    $multisort_params[] = &${"param_$i"};
  }
  $multisort_params[] = &$array; 

  call_user_func_array("array_multisort", $multisort_params);

  return $array;
}


/*******************************/
##Fetch all admin users from database
/******************************/
if($_GET['q']=='postjobs')
{
	$moduelArray=$moduleObj->getModuleHelpText(16);
	$smarty->assign('moduelArray', $moduelArray);
}
$userid= base64_decode($_GET['userid']);


## Get search parameters in variables - 

if($_POST['jobtype']!='0' && $_POST['jobtype']!='')
{
	$jobtype = $_POST['jobtype']; 
} 
elseif($_GET['jobtype']!='' && $_GET['jobtype']!='JOBTYPE')
{
	$jobtype =$_GET['jobtype']; 
}
else {
	$jobtype = '';
}
if($_POST['budget']!='0' && $_POST['budget']!=''){
	$budget = $_POST['budget'];
} 
elseif($_GET['budget']!='' && $_GET['budget']!='BUDGET')
{
	$budget =$_GET['budget']; 
}
else 
{
	$budget = '';
}
if($_POST['project_status']!='0' && $_POST['project_status']!=''){
	$Projectstatus = $_POST['project_status']; 
}
elseif($_GET['project_status']!='' && $_GET['project_status']!='PROJECTSTAUS')
{
	$Projectstatus =$_GET['project_status']; 
}
else {
	$Projectstatus = '';
}

/*if($_GET['col1'] != '') 
{
	$col1 = base64_decode($_GET['col1']);
} 
else if($_GET['col2'] != '') 
{
	$col2 = base64_decode($_GET['col2']);
} 
else 
{
	$col1 = '0';	
}
*/

##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;
	if($_GET['col1']!='' || $_GET['col2']!='')
	{
		if($_GET['col1'] != '') 
		{
			$pageName = "home.php?q=jobleads&userid=".$_GET['userid']."&col1=".$_GET['col1'].""; ;
		} 
		else if($_GET['col2'] != '') 
		{
			$pageName = "home.php?q=jobleads&userid=".$_GET['userid']."&col2=".$_GET['col2'].""; ;
		} 
		else 
		{
			$pageName = "home.php?q=jobleads&userid=".$_GET['userid'].""; 
		} 
	}
	else
	{
		$pageName ="home.php?q=jobleads&userid=".$_GET['userid']."";
		if(($_POST['jobtype']!='0' || $_GET['jobtype']!='' )&& $_GET['jobtype']!='JOBTYPE')
			$pageName  .= "&jobtype=".$jobtype."";
		else	
			$pageName  .= "&jobtype=JOBTYPE";
			
		if(($_POST['budget']!='0' || $_GET['budget']!='' )&& $_GET['budget']!='BUDGET')
			$pageName  .= "&budget=".$budget."";
		else	
			$pageName  .= "&budget=BUDGET";

		if(($_POST['project_status']!='0' || $_GET['project_status']!='') && $_GET['project_status']!='PROJECTSTAUS')
			$pageName  .= "&project_status=".$Projectstatus."";
		else	
			$pageName  .= "&project_status=PROJECTSTAUS";
		
	}
	
	## Count all the records
	$leadsArray = $userObj->leadsAccordingToUserId($userid,$jobtype,$budget,$Projectstatus,'','',false);
	$total_rows = count($leadsArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##


##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);
	$leadsArray = $userObj->leadsAccordingToUserId($userid,$jobtype,$budget,$Projectstatus,$rowsPerPage,$offset,true);
	//echo "<pre>"; print_r($leadsArray);exit;
	for($i=0;$i<count($leadsArray);$i++)
	{
		if($leadsArray[$i]['trade_id']!='')
		{
			$gettradedetail = $tradeObj->getTradeCategoryDetailsById($leadsArray[$i]['trade_id']);
			$leadsArray[$i]['tradename'] = $gettradedetail['trade_category'];
		}
		
		if($leadsArray[$i]['jobtype_id']!='')
		{
			$getjobtypedetail = $tradeObj->getTradeCategoryDetailsById($leadsArray[$i]['jobtype_id']);
			$leadsArray[$i]['jobtypename'] = $getjobtypedetail['trade_category'];
		}
		
		if($leadsArray[$i]['approx_budget']!='')
		{
			$getbudgetdetail = $budgetObj->getBudgetDetailsById($leadsArray[$i]['approx_budget']);
			$leadsArray[$i]['budgetname'] = $getbudgetdetail['budget_name'];
		}		
	}
	/*//echo "<pre>"; print_r($leadsArray);exit;
	if($col2=='0')
	{
		$leadsArray =  msort('jobtype_name', SORT_ASC, SORT_STRING,  $leadsArray);
//array_sort_by_column($leadsArray,'jobtype_name');
		$leadsArray = @array_values($leadsArray);
	}
	if($col2=='1')
	{
		$leadsArray = msort('jobtype_name', SORT_DESC, SORT_STRING, $leadsArray);//array_sort_by_column($leadsArray,'jobtype_name');
		$leadsArray = @array_values($leadsArray);
	}*/
	
	$smarty->assign('leadsArray', $leadsArray);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	}
	elseif($search_jobid != '') {
		$other_id = "search_jobid=".$search_jobid;
	} else {
		$other_id = '';
	}
	
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	

## Fetch user details by id
if($_GET['userid'] != '') {
	$user_id = base64_decode($_GET['userid']);	
	$user_details = $userObj->getUserDetailsByUserId($user_id);
	$smarty->assign('user_details', $user_details);
}
$getallTradeJobType = $tradeObj->getAllJobTypeWithActiveStatus();
$smarty->assign("getallTradeJobType", $getallTradeJobType);	

$getallBudget = $budgetObj->getAllBudgetWithActiveStatus();
$smarty->assign("getallBudget", $getallBudget);	
## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'joblead');
$smarty->assign('mainmenu', '7');

## Unset all the objects created which are on this page
unset($userObj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'joblead/jobleads.tpl');
unset($smarty);
?>